﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp8
{
    class LinkList<T> : ILinkable<T>
    {
        private LinkNode<T> first;
        public LinkNode<T> First
        {
            get { return first; }
            set { first = value; }
        }

        public T this[int index]
        {
            get
            {
                LinkNode<T> temp = first;
                for (int i = 0; i <= index; i++)
                {
                    temp = temp.Next;
                }
                return temp.Data;
            }
        }

        public T RemoveAt(int index)
        {
            T data = default(T);
            if (index==0)
            {
                data = first.Data;
                first = first.Next;
            }
            else
            {
                LinkNode<T> temp = first;
                for(int i=1;i<=index-1;i++)
                {
                    temp = temp.Next;

                }
                LinkNode<T> preNode = temp;
                LinkNode<T> currentNode = temp.Next;
                data = currentNode.Data;
                LinkNode<T> postNode = temp.Next.Next;
                preNode.Next = postNode;
            }
            return data;
        }
        public void InsertAt(T item, int index)
        {
            LinkNode<T> newNode = new LinkNode<T>(item);
            if(index == 0)
            {
                newNode.Next = first;
                first = newNode;
            }
            else
            {
                LinkNode<T> temp = first;
                for (int i = 0; i <= index-1; i++)
                {
                    temp = temp.Next;
                }

             LinkNode<T> preNode = temp;
            LinkNode<T> postNode = temp.Next;
            preNode.Next = newNode;
            newNode.Next = postNode;
            }

        }

        public bool isEmpty()
        {
           return first==null;
        }

        public int Count
        {
            get
            {
                int i = 0;
                LinkNode<T> node = first;
                while(node.Next!=null)
                {
                    node = node.Next;
                    i++;
                }
                return i;
            }
        }
        public LinkNode<T> GetNode(int pos)
        {
            LinkNode<T> node = first;
            for (int i = 0; i < pos; i++)
            {
                if(node.Next==null)
                {
                    throw new IndexOutOfRangeException();

                }
                node = node.Next;

            }
            return node;
        }
        public int IndexOf(T value)
        {
            LinkNode<T> temp = first;
            if(first==null)
            {
                return -1;
            }
            else
            {
                int index = 0;
                while(true)
                {
                    if (temp.Data.Equals(value))
                        return index;
                    else
                    {
                        if (temp.Next != null)
                        {
                            temp = temp.Next;
                        }
                        else break;
                    }
                }
                return -1;
            }
        }
    }
}
